package com.ruoyi.tws.controller.app;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.tws.domain.AppVersion;
import com.ruoyi.tws.service.IAppVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL + "/")
public class VersionController extends BaseController {

    private final IAppVersionService iAppVersionService;

    /**
     * 检查APP版本
     * @param type 平台（android/ios）
     * @param versionCode APP版本号
     * @return
     */
    @GetMapping("checkVersion")
    public R<AppVersion> checkVersion(@RequestParam(value = "type") String type, @RequestParam(value = "versionCode") int versionCode) {
        List<AppVersion> apps = iAppVersionService.lambdaQuery()
                                    .eq(AppVersion::getType, type)
                                    .gt(AppVersion::getVersionNo, versionCode)
                                    .orderByDesc(AppVersion::getVersionNo)
                                    .list();
        if(apps != null && apps.size() > 0) {
            return R.ok(apps.get(0));
        }
        return R.ok();
    }

}
